﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HorizontalScrollCircle : BaseScrollCircle
{

    [SerializeField]
    protected RectOffset m_Padding;
    [SerializeField]
    protected float m_Space;
    [SerializeField]
    protected TextAnchor m_ChildAlignment;
    protected RectTransform rt { get { if (_rt == null) _rt = gameObject.GetComponent<RectTransform>(); return _rt; } }
    private RectTransform _rt;

    protected Vector2 CSIZE;
    protected override void OnRefrashPos(ScrollCircleItem[] items, RectTransform content)
    {
        //Vector3 lpl = content.InverseTransformPoint(rt.TransformPoint(new Vector3(rt.rect.x, rt.rect.y + rt.rect.height, 0)));
        //Vector3 lpr = content.InverseTransformPoint(rt.TransformPoint(new Vector3(rt.rect.x + rt.rect.width, rt.rect.y, 0)));
        //for (int i = 0; i < items.Length; i++)
        //{
        //    ScrollCircleItem item = items[i];
        //    Vector2 sz = item.GetSize();
        //    Vector2 pos = item.GetPos();
        //    if (pos.x - m_Space - sz.x < lpr.x && pos.x + sz.x*2 + m_Space > lpl.x)
        //        item.OnShow();
        //    else
        //        item.OnHide();
        //}

        for (int i = 0; i < items.Length; i++)
        {
            ScrollCircleItem item = items[i];
            Vector3 llb = rt.InverseTransformPoint(content.TransformPoint(new Vector3(item.rect.x - item.rect.width * 0.5f - Mathf.Abs(m_Space),
                item.rect.y, 0)));
            Vector3 lrt = rt.InverseTransformPoint(content.TransformPoint(new Vector3(item.rect.x + item.rect.width * 1.5f + Mathf.Abs(m_Space),
                item.rect.y + item.rect.height, 0)));
            Rect itemt = new Rect(llb.x, llb.y, Mathf.Abs(lrt.x - llb.x), Mathf.Abs(lrt.y - llb.y));
            if (rt.rect.Overlaps(itemt))
                item.OnShow();
            else
                item.OnHide();
        }
    }
    protected override void OnDrawGizmos()
    {
        if (IsDraw)
        {
            Gizmos.color = Color.blue;
            Vector3 wl = rt.TransformPoint(new Vector3(rt.rect.x, rt.rect.y + rt.rect.height, 0));
            Vector3 wr = rt.TransformPoint(new Vector3(rt.rect.x + rt.rect.width, rt.rect.y, 0));

            Gizmos.DrawLine(wl, wl + Vector3.right * Mathf.Abs(wr.x - wl.x));
            Gizmos.DrawLine(wr, wr + Vector3.left * Mathf.Abs(wr.x - wl.x));
            Gizmos.DrawLine(wl, wl + Vector3.down * Mathf.Abs(wr.y - wl.y));
            Gizmos.DrawLine(wr, wr + Vector3.up * Mathf.Abs(wr.y - wl.y));
        }
        base.OnDrawGizmos();
    }
    protected override void OnRefrashSize(ScrollCircleItem[] items, RectTransform content)
    {
        int findex = 0;
        Vector2 SIZE = new Vector2(0, 0);
        for (int i = 0; i < items.Length; i++)
        {
            findex++;
            Vector2 sz = items[i].GetSize();
            SIZE.x += sz.x;
            SIZE.y = Mathf.Max(SIZE.y, sz.y);
        }
        if (items.Length > 0)
            SIZE.x += ((findex - 1) * m_Space);
        CSIZE = SIZE;
        CSIZE.x = SIZE.x + m_Padding.left + m_Padding.right;
        CSIZE.y = SIZE.y + m_Padding.top + m_Padding.bottom;
        CSIZE = new Vector2(Mathf.Max(0, CSIZE.x), Mathf.Max(0, CSIZE.y));
        content.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, CSIZE.x);
        Vector2 pltPos = new Vector2(0, 0);
        Vector2 prbPos = new Vector2(CSIZE.x, -CSIZE.y);

        Vector2 cltPos = new Vector2(pltPos.x + m_Padding.left, pltPos.y - m_Padding.top);
        Vector2 crbPos = new Vector2(prbPos.x - m_Padding.right, prbPos.y + m_Padding.bottom);
      
        int offset = 1;
        Vector2 nextPos = new Vector2(cltPos.x, cltPos.y);
        switch (m_ChildAlignment)
        {
            case TextAnchor.UpperLeft:
                offset = -1;
                nextPos = new Vector2(cltPos.x, cltPos.y);

                break;
            case TextAnchor.UpperCenter:
                offset = -1;
                nextPos = new Vector2(cltPos.x + ((crbPos.x - cltPos.x) / 2.0f - SIZE.x / 2.0f), cltPos.y);
                break;
            case TextAnchor.UpperRight:
                offset = -1;
                nextPos = new Vector2(crbPos.x - SIZE.x, cltPos.y);
                break;
            case TextAnchor.LowerLeft:
                offset = 1;
                nextPos = new Vector2(cltPos.x, crbPos.y);
                break;
            case TextAnchor.LowerCenter:
                offset = 1;
                nextPos = new Vector2(cltPos.x + ((crbPos.x - cltPos.x) / 2.0f - SIZE.x / 2.0f), crbPos.y);
                break;
            case TextAnchor.LowerRight:
                offset = 1;
                nextPos = new Vector2(crbPos.x - SIZE.x, crbPos.y);
                break;
            case TextAnchor.MiddleLeft:
                offset = 0;
                nextPos = new Vector2(cltPos.x, crbPos.y + (cltPos.y - crbPos.y) / 2.0f);

                break;
            case TextAnchor.MiddleCenter:
                offset = 0;
                nextPos = new Vector2(cltPos.x + ((crbPos.x - cltPos.x) / 2.0f - SIZE.x / 2.0f), crbPos.y + (cltPos.y - crbPos.y) / 2.0f);
                break;
            case TextAnchor.MiddleRight:
                offset = 0;
                nextPos = new Vector2(crbPos.x - SIZE.x, crbPos.y + (cltPos.y - crbPos.y) / 2.0f);
                break;
        }

        for (int i = 0; i < items.Length; i++)
        {
            Vector2 sz = items[i].GetSize();
            items[i].SetPos(new Vector2(nextPos.x, nextPos.y + offset * sz.y / 2.0f + sz.y / 2.0f));
            nextPos.x = nextPos.x + sz.x + m_Space;
        }

    }
}
